﻿@model NaviCar.Models.HistoricoNavegacao
@{
    ViewBag.Title = "Historico";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section InfoUser {
    @Html.Partial("InfoUser")
}

@section Scripts{
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var map;
        var rastreando = false;
        var codInt = 0;
        var intervalo = null;

        $(document).ready(function () {

            var mapa = $("#map_canvas").get(0);

            var latlng = new google.maps.LatLng(41.875696, -87.624207);
            var myOptions = {
                zoom: 11,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            map = new google.maps.Map(mapa, myOptions);

            //            var ctaLayer = new google.maps.KmlLayer('https://t3n6yw.bay.livefilestore.com/y1ppz3mZ8lvHS-kNmk42FLyZMKdlFf3veZT7J3r6j_9Sy12N8OjoeB1p1lbEMSNrHOycc3t88RV5gIAj8xDY5f7fDHYdHMbKzaT/KM.kml');
            //            ctaLayer.setMap(map);
        });

        function iniciar() {
           
            var cod = $("#Veiculo").val();
            var inicio = $("#Inicio").val();
            var fim = $("#Fim").val();
            
            if(inicio == "" || fim == ""){
                alert("A Data de início e fim são obrigatórias.");
                return;
            }

            if (cod == "") {
                alert("Selecione o veículo.");
                return;
            }
            
            var ctaLayer = new google.maps.KmlLayer(rebuidURL(cod,inicio,fim));
            ctaLayer.setMap(map);
            $("#lnkKM").attr("href", rebuidURL(cod, inicio, fim));

        }

        function rebuidURL(id,inicio,fim){

            pathArray = window.location.href.split('/');
            newPathname = "";

            pathArray[pathArray.length - 1] = "PesquisaHistorico";
            pathArray[pathArray.length - 2] = "Navegacao";
            pathArray[pathArray.length] = id;
            pathArray[pathArray.length] = inicio.replace("/", "-").replace("/", "-").replace("/", "-");
            pathArray[pathArray.length] = fim.replace("/", "-").replace("/", "-").replace("/", "-");
            pathArray[pathArray.length] = "o.kml";

            for (i = 0; i < pathArray.length; i++) {
                if(pathArray[i] != "")
                {
                    newPathname += "/";
                    newPathname += pathArray[i];
                }
            }

            return newPathname.replace("/http:/", "http://");
        }
    </script>
}
<div>
    @Html.ActionLink("KML","Historico1")
    <a id="lnkKM" href="">Teste</a>
    <span>@Html.LabelFor(m=>m.Veiculo) &nbsp; @Html.EditorFor(m => m.Veiculo)</span>
    <span>@Html.LabelFor(m=>m.Inicio) &nbsp; @Html.EditorFor(m => m.Inicio)</span>
    <span>@Html.LabelFor(m=>m.Fim) &nbsp; @Html.EditorFor(m => m.Fim)</span>
    <input type="button" value="Ver No mapa" onclick="javascript:iniciar();" />

</div>
<div id="map_canvas" style="width: 100%; height: 600px"></div>
